#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a);i<n;i++)
int a[200100],h[1000100],d[200100];
long long f[200100];
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--){
		long long ans=0;
		int n;
		scanf("%d",&n);
		memset(h,-1,sizeof(h));
		REP(i,0,n){
			scanf("%d",&a[i]);
			if(i&&a[i]==a[i-1]){
				ans+=a[i];
				i--;
				n--;
				continue;
			}
			d[i]=h[a[i]];
			h[a[i]]=i;
		}
		f[0]=0;
		REP(i,0,n){
			if(i)f[i]=f[i-1];
			if(d[i]>=0){
				f[i]=max(f[i],f[d[i]+1]+a[i]);
			}
		}
		printf("%lld\n",f[n-1]+ans);
	}
	return 0;
}
